<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE html 
     PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
  <title>Module: ActionView::Helpers::CaptureHelper</title>
  <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
  <meta http-equiv="Content-Script-Type" content="text/javascript" />
  <link rel="stylesheet" href="../../.././rdoc-style.css" type="text/css" media="screen" />
  <script type="text/javascript">
  // <![CDATA[

  function popupCode( url ) {
    window.open(url, "Code", "resizable=yes,scrollbars=yes,toolbar=no,status=no,height=150,width=400")
  }

  function toggleCode( id ) {
    if ( document.getElementById )
      elem = document.getElementById( id );
    else if ( document.all )
      elem = eval( "document.all." + id );
    else
      return false;

    elemStyle = elem.style;
    
    if ( elemStyle.display != "block" ) {
      elemStyle.display = "block"
    } else {
      elemStyle.display = "none"
    }

    return true;
  }
  
  // Make codeblocks hidden by default
  document.writeln( "<style type=\"text/css\">div.method-source-code { display: none }</style>" )
  
  // ]]>
  </script>

</head>
<body>



    <div id="classHeader">
        <table class="header-table">
        <tr class="top-aligned-row">
          <td><strong>Module</strong></td>
          <td class="class-name-in-header">ActionView::Helpers::CaptureHelper</td>
        </tr>
        <tr class="top-aligned-row">
            <td><strong>In:</strong></td>
            <td>
                <a href="../../../files/vendor/rails/actionpack/lib/action_view/helpers/capture_helper_rb.html">
                vendor/rails/actionpack/lib/action_view/helpers/capture_helper.rb
                </a>
        <br />
            </td>
        </tr>

        </table>
    </div>
  <!-- banner header -->

  <div id="bodyContent">



  <div id="contextContent">

    <div id="description">
      <p>
<a href="CaptureHelper.html">CaptureHelper</a> exposes methods to let you
extract generated markup which can be used in other parts of a template or
layout file. It provides a method to <a
href="CaptureHelper.html#M002146">capture</a> blocks into variables through
<a href="CaptureHelper.html#M002146">capture</a> and a way to <a
href="CaptureHelper.html#M002146">capture</a> a block of markup for use in
a layout through <a href="CaptureHelper.html#M002147">content_for</a>.
</p>

    </div>


   </div>

    <div id="method-list">
      <h3 class="section-bar">Methods</h3>

      <div class="name-list">
      <a href="#M002146">capture</a>&nbsp;&nbsp;
      <a href="#M002147">content_for</a>&nbsp;&nbsp;
      </div>
    </div>

  </div>


    <!-- if includes -->

    <div id="section">





      


    <!-- if method_list -->
    <div id="methods">
      <h3 class="section-bar">Public Instance methods</h3>

      <div id="method-M002146" class="method-detail">
        <a name="M002146"></a>

        <div class="method-heading">
          <a href="#M002146" class="method-signature">
          <span class="method-name">capture</span><span class="method-args">(*args, &amp;block)</span>
          </a>
        </div>
      
        <div class="method-description">
          <p>
The <a href="CaptureHelper.html#M002146">capture</a> method allows you to
extract part of a template into a variable. You can then use this variable
anywhere in your templates or layout.
</p>
<h4>Examples</h4>
<p>
The <a href="CaptureHelper.html#M002146">capture</a> method can be used in
ERb templates&#8230;
</p>
<pre>
  &lt;% @greeting = capture do %&gt;
    Welcome to my shiny new web page!  The date and time is
    &lt;%= Time.now %&gt;
  &lt;% end %&gt;
</pre>
<p>
&#8230;and <a href="../../Builder.html">Builder</a> (RXML) templates.
</p>
<pre>
  @timestamp = capture do
    &quot;The current timestamp is #{Time.now}.&quot;
  end
</pre>
<p>
You can then use that variable anywhere else. For example:
</p>
<pre>
  &lt;html&gt;
  &lt;head&gt;&lt;title&gt;&lt;%= @greeting %&gt;&lt;/title&gt;&lt;/head&gt;
  &lt;body&gt;
  &lt;b&gt;&lt;%= @greeting %&gt;&lt;/b&gt;
  &lt;/body&gt;&lt;/html&gt;
</pre>
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M002146-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M002146-source">
<pre>
    <span class="ruby-comment cmt"># File vendor/rails/actionpack/lib/action_view/helpers/capture_helper.rb, line 33</span>
33:       <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">capture</span>(<span class="ruby-operator">*</span><span class="ruby-identifier">args</span>, <span class="ruby-operator">&amp;</span><span class="ruby-identifier">block</span>)
34:         <span class="ruby-comment cmt"># Return captured buffer in erb.</span>
35:         <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">block_called_from_erb?</span>(<span class="ruby-identifier">block</span>)
36:           <span class="ruby-identifier">with_output_buffer</span> { <span class="ruby-identifier">block</span>.<span class="ruby-identifier">call</span>(<span class="ruby-operator">*</span><span class="ruby-identifier">args</span>) }
37:         <span class="ruby-keyword kw">else</span>
38:           <span class="ruby-comment cmt"># Return block result otherwise, but protect buffer also.</span>
39:           <span class="ruby-identifier">with_output_buffer</span> { <span class="ruby-keyword kw">return</span> <span class="ruby-identifier">block</span>.<span class="ruby-identifier">call</span>(<span class="ruby-operator">*</span><span class="ruby-identifier">args</span>) }
40:         <span class="ruby-keyword kw">end</span>
41:       <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>

      <div id="method-M002147" class="method-detail">
        <a name="M002147"></a>

        <div class="method-heading">
          <a href="#M002147" class="method-signature">
          <span class="method-name">content_for</span><span class="method-args">(name, content = nil, &amp;block)</span>
          </a>
        </div>
      
        <div class="method-description">
          <p>
Calling <a href="CaptureHelper.html#M002147">content_for</a> stores a block
of markup in an identifier for later use. You can make subsequent calls to
the stored content in other templates or the layout by passing the
identifier as an argument to <tt>yield</tt>.
</p>
<h4>Examples</h4>
<pre>
  &lt;% content_for :not_authorized do %&gt;
    alert('You are not authorized to do that!')
  &lt;% end %&gt;
</pre>
<p>
You can then use <tt>yield :not_authorized</tt> anywhere in your templates.
</p>
<pre>
  &lt;%= yield :not_authorized if current_user.nil? %&gt;
</pre>
<p>
You can also use this syntax alongside an existing call to <tt>yield</tt>
in a layout. For example:
</p>
<pre>
  &lt;%# This is the layout %&gt;
  &lt;html xmlns=&quot;http://www.w3.org/1999/xhtml&quot; xml:lang=&quot;en&quot; lang=&quot;en&quot;&gt;
  &lt;head&gt;
    &lt;title&gt;My Website&lt;/title&gt;
    &lt;%= yield :script %&gt;
  &lt;/head&gt;
  &lt;body&gt;
    &lt;%= yield %&gt;
  &lt;/body&gt;
  &lt;/html&gt;
</pre>
<p>
And now, we&#8216;ll create a view that has a <a
href="CaptureHelper.html#M002147">content_for</a> call that creates the
<tt>script</tt> identifier.
</p>
<pre>
  &lt;%# This is our view %&gt;
  Please login!

  &lt;% content_for :script do %&gt;
    &lt;script type=&quot;text/javascript&quot;&gt;alert('You are not authorized to view this page!')&lt;/script&gt;
  &lt;% end %&gt;
</pre>
<p>
Then, in another view, you could to do something like this:
</p>
<pre>
  &lt;%= link_to_remote 'Logout', :action =&gt; 'logout' %&gt;

  &lt;% content_for :script do %&gt;
    &lt;%= javascript_include_tag :defaults %&gt;
  &lt;% end %&gt;
</pre>
<p>
That will place &lt;script&gt; tags for Prototype, Scriptaculous, and
application.js (if it exists) on the page; this technique is useful if
you&#8216;ll only be using these scripts in a few views.
</p>
<p>
Note that <a href="CaptureHelper.html#M002147">content_for</a> concatenates
the blocks it is given for a particular identifier in order. For example:
</p>
<pre>
  &lt;% content_for :navigation do %&gt;
    &lt;li&gt;&lt;%= link_to 'Home', :action =&gt; 'index' %&gt;&lt;/li&gt;
  &lt;% end %&gt;

  &lt;%#  Add some other content, or use a different template: %&gt;

  &lt;% content_for :navigation do %&gt;
    &lt;li&gt;&lt;%= link_to 'Login', :action =&gt; 'login' %&gt;&lt;/li&gt;
  &lt;% end %&gt;
</pre>
<p>
Then, in another template or layout, this code would render both links in
order:
</p>
<pre>
  &lt;ul&gt;&lt;%= yield :navigation %&gt;&lt;/ul&gt;
</pre>
<p>
Lastly, simple content can be passed as a parameter:
</p>
<pre>
  &lt;% content_for :script, javascript_include_tag(:defaults) %&gt;
</pre>
<p>
WARNING: <a href="CaptureHelper.html#M002147">content_for</a> is ignored in
caches. So you shouldn&#8216;t use it for elements that will be fragment
cached.
</p>
<p>
The deprecated way of accessing a <a
href="CaptureHelper.html#M002147">content_for</a> block is to use an
instance variable named <tt>@content_for_#{name_of_the_content_block}</tt>.
The preferred usage is now <tt>&lt;%= yield :footer %&gt;</tt>.
</p>
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M002147-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M002147-source">
<pre>
     <span class="ruby-comment cmt"># File vendor/rails/actionpack/lib/action_view/helpers/capture_helper.rb, line 118</span>
118:       <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">content_for</span>(<span class="ruby-identifier">name</span>, <span class="ruby-identifier">content</span> = <span class="ruby-keyword kw">nil</span>, <span class="ruby-operator">&amp;</span><span class="ruby-identifier">block</span>)
119:         <span class="ruby-identifier">ivar</span> = <span class="ruby-node">&quot;@content_for_#{name}&quot;</span>
120:         <span class="ruby-identifier">content</span> = <span class="ruby-identifier">capture</span>(<span class="ruby-operator">&amp;</span><span class="ruby-identifier">block</span>) <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">block_given?</span>
121:         <span class="ruby-identifier">instance_variable_set</span>(<span class="ruby-identifier">ivar</span>, <span class="ruby-node">&quot;#{instance_variable_get(ivar)}#{content}&quot;</span>)
122:         <span class="ruby-keyword kw">nil</span>
123:       <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>


    </div>


  </div>


<div id="validator-badges">
  <p><small><a href="http://validator.w3.org/check/referer">[Validate]</a></small></p>
</div>

</body>
</html>